// 引入koa-router插件
const Router = require('koa-router');
const mongoose = require('mongoose');
const svgCaptcha = require('svg-captcha'); // 验证码

// 实例化router
const userRouter = new Router();

function setCookies(millisecond) {
  return {
    path: '/',
    maxAge: millisecond,
    httpOnly: false,
    overwrite: false,
  }
}

userRouter.post('/register', async (ctx) => {
  try {
    const User = mongoose.model('User'); // 获取User模型
    const userInfo = ctx.request.body;
    const newUser = new User(userInfo);
    await newUser.save();
    ctx.cookies.set('user', userInfo.userName, setCookies(30 * 60 * 1000));
    // 返回给前端的数据
    ctx.body = {
      code: 200,
      message: '注册成功',
    }
  } catch (error) {
    // 返回给前端的数据
    ctx.body = {
      code: 500,
      message: error,
    };
  }
})

// 登录接口
userRouter.post('/login', async (ctx) => {
  // 获取客户端的用户信息
  const userInfo = ctx.request.body;
  const userName = userInfo.userName;
  const password = userInfo.password;
  const User = mongoose.model('User');
  // 根据userName获取数据库的密码
  try {
    const result = await User.findOne({ userName: userName }).exec();
    if (result) {
      // 进行对比密码
      if (password === result.password) {
        ctx.cookies.set('user', userName, setCookies(30 * 60 * 1000))
        ctx.body = {
          code: 200,
          message: '登录成功',
          isMatch: true,
        }
      } else {
        ctx.body = {
          code: 200,
          message: '密码错误',
          isMatch: false,
        }
      }
    } else {
      ctx.body = {
        code: 200,
        message: '用户名不存在',
        isMatch: false,
      }
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '登录失败',
    }
  }
})

// 查找当前用户
userRouter.get('/getCurrentUser', async (ctx) => {
  try {
    const User = mongoose.model('User');
    const userName = ctx.request.query.userName;
    const res = await User.findOne({ userName }).exec();
    ctx.body = {
      code: 200,
      message: res,
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: error,
    }
  }
})

// 根据用户名,修改当前用户
userRouter.put('/saveMemberInfo', async (ctx) => {
  try {
    const User = mongoose.model('User');
    const userId = ctx.request.body.userId;
    delete ctx.request.body.userId;
    // 更新
    await User.updateOne({ _id: userId }, {$set: ctx.request.body});
    ctx.body = {
      code: 200,
      message: '修改成功',
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: error,
    }
  }
})

// 用户退出
userRouter.get('/loginOut', async(ctx) => {
  try {
    ctx.cookies.set('user', '', setCookies(0))
    ctx.body = {
      code: 200,
      message: '退出成功',
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '退出失败',
    }
  }
})

// 注销用户
userRouter.delete('/writeOffUser', async (ctx) => {
  try {
    const User = mongoose.model('User');
    const { userId } = ctx.request.query;
    await User.remove({_id: userId});
    ctx.cookies.set('user', '', setCookies(0))
    ctx.body = {
      code: 200,
      message: '注销成功',
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '注销失败',
    }
  }
})

// 获取验证码
userRouter.get('/getCaptcha', async (ctx) => {
  const res = svgCaptcha.create();
  ctx.body = {
    code: 200,
    decodeImg: res.data, // svg的结构
    txtValue: res.text, // captcha的文案
  };
})

module.exports = userRouter;